package cn.hl.hm.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hl.hm.dao.DriverMapper;
import cn.hl.hm.dao.OrderSeqMapper;
import cn.hl.hm.dao.RoadFeeMapper;
import cn.hl.hm.entity.OrderSeq;
import cn.hl.hm.entity.RoadFee;
import cn.hl.hm.service.RoadFeeService;
import cn.hl.hm.util.wxjs.BizOrder;
import cn.hl.hm.util.wxjs.CommonUtil;
import cn.hl.hm.util.wxjs.PayInfo;
@Service
public class RoadFeeServiceImpl implements RoadFeeService{
	private static final Logger logger = LoggerFactory.getLogger(RoadFeeServiceImpl.class);
	@Autowired
	private RoadFeeMapper roadFeeMapper;
	
	@Autowired
	private DriverMapper driverMapper;
	private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyyMMdd");
	
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	@Autowired
	private OrderSeqMapper om;
	@Override
	public int add(RoadFee roadFee) {
		return roadFeeMapper.insertSelective(roadFee);
	}



    /**
     * @Title: wxScanMer  
     * @Description: 微信扫码原生支付
     * @date: 2017年3月27日
     * @author:大和
     * @param orderInfo
     * @param ip
     * @return 	Map<String, String>
     * @throws
    */
   public 	Map<String, String> wxScanMer(RoadFee orderInfo) {
		BizOrder bo = new BizOrder();
		//组装订单数据
     bo.setId(orderInfo.getId().toString());
     bo.setFeeAmount((int)(orderInfo.getPayPrice().setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()*100));
     bo.setOrderCode(orderInfo.getOrderCode());
     bo.setSeriNum(orderInfo.getReciviceMoneySerialNumber());
     logger.info("订单总支付金额:"+bo.getFeeAmount());

	  PayInfo pi = CommonUtil.createPayInfo(bo,orderInfo.getRemark(),null);
	  String sign;
	  Map<String, String> map = null;
	try {
		sign = CommonUtil.getSign(pi);
		pi.setSign(sign);

		//3 调用统一下单接口
		logger.info(CommonUtil.payInfoToXML(pi).replace("__", "_"));
		 map = CommonUtil.httpsRequestToXML("https://api.mch.weixin.qq.com/pay/unifiedorder", "POST", CommonUtil.payInfoToXML(pi).replace("__", "_").replace("<![CDATA[", "").replace("]]>", ""));
	 	logger.info(map.toString());
	 	
	 	
	 	
		 
	} catch (Exception e) {
		e.printStackTrace();
	}
	return map;

   }


	@Override
	public List<RoadFee> selectPageList(RoadFee roadFee) {
		return roadFeeMapper.selectRoadFees(roadFee);
	}



	@Override
	public int update(RoadFee roadFee) {
		return roadFeeMapper.updateByPrimaryKeySelective(roadFee);
	}
	
	
	@Override
	public String generateOrderCode(String roadFeeType) {
		Integer carNum = null;
		if(roadFeeType.contains("重型牵引")){
			carNum = 88;
		}else if (roadFeeType.contains("重型货车")) {
			carNum = 77;
		}else if (roadFeeType.contains("八平")) {
			carNum = 66;
		}else if (roadFeeType.contains("二桥")){
			carNum = 55;

		}else if (roadFeeType.contains("免费")){
			carNum = 99;

		}else {
			return null;
		}
		StringBuffer orderCode = new StringBuffer();
		Date date = new Date();
		orderCode.append(shortSdf.format(date).substring(2));
		
		OrderSeq os = new OrderSeq();
		String format = sdf.format(date);
		os.setCreateTime(format);
		OrderSeq selectOrderSeqNum = om.selectOrderSeqNum(os);
		Integer seqNum = 1;
		if(selectOrderSeqNum!=null ){
			// 当天存在，获取seq_num
			seqNum = selectOrderSeqNum.getSeqNum();
			seqNum++;
			orderCode.append(String.format("%04d", seqNum));
		}else{
			// 不存在，从00001 开始
			orderCode.append(String.format("%04d", seqNum));
		}
		// 设置序列号，新增序列表
		os.setSeqNum(seqNum);
		// 添加订单成功，将seqNum保存至 order_seq 表中
		os.setCreateTime(format);
		int insertSelective = om.insertSelective(os);		
		if(insertSelective>0){
			return orderCode.append(carNum).toString();
		}else {
			return null;
		}
	}

	@Override
    public Integer selectPayTimeBefore20MINUTE(Integer driverId,String plateNumber) {
        return roadFeeMapper.selectPayTimeBefore20MINUTE(driverId,plateNumber);
    }

    public static void main(String[] args) {
		System.out.println(sdf.format(new Date()));
	}
}
